package com.example.a2023110445

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.Button
import android.widget.GridView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.a2023110445.model.Card
import com.example.a2023110445.model.CardMatchingGame
import com.example.a2023110445.model.Player

class CardGameActivity : AppCompatActivity() {
    
    private lateinit var gvCards: GridView
    private lateinit var tvScore: TextView
    private lateinit var btnRestart: Button
    private lateinit var btnBack: Button
    
    private lateinit var game: CardMatchingGame
    private lateinit var cardAdapter: CardAdapter
    private lateinit var player: Player
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_card_game)
        
        player = intent.getParcelableExtra<Player>(MainActivity.EXTRA_PLAYER) ?: Player()
        
        // 初始化游戏模型（12张牌）
        game = CardMatchingGame(12)
        
        initViews()
        
        cardAdapter = CardAdapter()
        gvCards.adapter = cardAdapter
        
        btnRestart.setOnClickListener {
            restartGame()
        }
        
        btnBack.setOnClickListener {
            finishGame()
        }
    }
    
    private fun initViews() {
        gvCards = findViewById(R.id.gvCards)
        tvScore = findViewById(R.id.tvScore)
        btnRestart = findViewById(R.id.btnRestart)
        btnBack = findViewById(R.id.btnBack)
        updateScore()
    }
    
    private fun updateScore() {
        val scoreText = getString(R.string.score_label) + game.score.toString()
        tvScore.text = scoreText
    }
    
    private fun restartGame() {
        game = CardMatchingGame(12)
        cardAdapter = CardAdapter()
        gvCards.adapter = cardAdapter
        updateScore()
    }
    
    private fun finishGame() {
        val resultIntent = Intent()
        resultIntent.putExtra(MainActivity.EXTRA_NEW_SCORE, game.score)
        setResult(RESULT_OK, resultIntent)
        finish()
    }
    
    inner class CardAdapter : BaseAdapter() {
        override fun getCount(): Int {
            return game.getCardCount()
        }
        
        override fun getItem(position: Int): Any {
            return game.cardAtIndex(position)
        }
        
        override fun getItemId(position: Int): Long {
            return position.toLong()
        }
        
        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            val card = game.cardAtIndex(position)
            val button: Button = if (convertView == null) {
                layoutInflater.inflate(R.layout.item_card, parent, false) as Button
            } else {
                convertView as Button
            }
            
            // 更新按钮显示
            if (card.isMatched) {
                button.text = card.toString()
                button.isEnabled = false
                button.alpha = 0.5f
            } else if (card.isChosen) {
                button.text = card.toString()
                button.isEnabled = true
                button.alpha = 1.0f
            } else {
                button.text = getString(R.string.card_back)
                button.isEnabled = true
                button.alpha = 1.0f
            }
            
            button.setOnClickListener {
                game.chooseCardAtIndex(position)
                notifyDataSetChanged()
                updateScore()
            }
            
            return button
        }
    }
}

